//
//  HomeAddViewController.m
//  GuoBuGaForIphone
//
//  Created by zhangwo on 16/10/27.
//  Copyright © 2016年 GuoBuGa. All rights reserved.
//

#import "HomeAddViewController.h"

@interface HomeAddViewController ()
{
    NSMutableArray *addArray;
}

@end

@implementation HomeAddViewController
@synthesize tableView = _tableView;
@synthesize addDelegate = _addDelegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [DSXUI barButtonWithStyle:DSXBarButtonStyleBack target:self action:@selector(back)];
    
    //搜索框
    _searchView = [[HomeSearchView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH-100, 26)];
    _searchView.textField.delegate = self;
    self.navigationItem.titleView = _searchView;
    
    //添加
//    addArray = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AddContent" ofType:@"plist"]];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tableView];

    [[DSXHttpManager sharedManager] GET:@"&c=ad&a=index" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject objectForKey:@"errno"] intValue] == 0) {
                _dataArray = [responseObject objectForKey:@"data"];
                [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    [_tableView registerClass:[AddTableViewCell class] forCellReuseIdentifier:@"addCell"];
}

- (void)back{
    if (![self.navigationController popViewControllerAnimated:YES]) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 22;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_dataArray objectAtIndex:indexPath.row];
    AddTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addCell"];
    cell.data = [_dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (![self hasItem:dic]) {
        cell.addButton.tag = indexPath.row;
        [cell.addButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.addButton setSelected:NO];
    }else{
        [cell.addButton setSelected:YES];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

#pragma mark -
- (BOOL)hasItem:(NSDictionary *)data{
    NSInteger menuid = [[data objectForKey:@"id"] integerValue];
    for (NSDictionary *dict in _selectedItems) {
        if (menuid == [[dict objectForKey:@"id"] intValue]) {
            return YES;
        }
    }
    return NO;
}

- (void)buttonClick:(UIButton *)button{
    [button setSelected:YES];
    [button removeTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];//移除点击事件
    NSDictionary *data = [_dataArray objectAtIndex:button.tag];
    if (_addDelegate && [_addDelegate respondsToSelector:@selector(addMenuItem:)]) {
        [_addDelegate addMenuItem:data];
    }
}

@end
